package net.oschina.foodon.examples.spring.cloud.service.gateway.filter;

import com.netflix.zuul.ZuulFilter;
import com.netflix.zuul.context.RequestContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;

/**
 * Created by foodon on 2017/4/10.
 */
@Component
public class PreFilter extends ZuulFilter {

    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    /*
        pre:请求执行之前filter
        route: 处理请求，进行路由
        post: 请求处理完成后执行的filter
        error:出现错误时执行的filter
     */
    @Override
    public String filterType() {
        return "pre";
    }

    @Override
    public int filterOrder() {
        return 100;
    }

    @Override
    public boolean shouldFilter() {
        return true;
    }

    @Override
    public Object run() {
        System.out.println("running javaPreFilter");

        RequestContext ctx = RequestContext.getCurrentContext();
        ctx.set("name", "liaokailin");

        HttpServletRequest request = ctx.getRequest();
        return null;
    }
}